/*
package com.byd.dilink.route.gateway.filter;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.rewrite.CachedBodyOutputMessage;
import org.springframework.cloud.gateway.support.BodyInserterContext;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.sql.Timestamp;

import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR;

*/
/**
 * @author: lai.quanqiang
 * @date : 2020/07/02
 *//*


@Component
@Slf4j
public class CommonResponseBodyGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> {

    @Autowired
    private CheckAuthUtils checkAuthUtils;

    @Override
    public GatewayFilter apply(Object config) {

        return new SecurityResponseGatewayFilter();
    }

    public class SecurityResponseGatewayFilter implements GatewayFilter, Ordered {

        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            return chain.filter(exchange.mutate().response(decorate(exchange)).build());
        }

        ServerHttpResponse decorate(ServerWebExchange exchange) {
            return new ServerHttpResponseDecorator(exchange.getResponse()) {

                @Override
                public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {

                    Class inClass = String.class;
                    Class outClass = String.class;

                    String originalResponseContentType = exchange.getAttribute(ORIGINAL_RESPONSE_CONTENT_TYPE_ATTR);
                    HttpHeaders httpHeaders = new HttpHeaders();

                    httpHeaders.add(HttpHeaders.CONTENT_TYPE, originalResponseContentType);

                    ClientResponse clientResponse = ClientResponse.create(exchange.getResponse().getStatusCode())
                            .headers(headers -> headers.putAll(httpHeaders)).body(Flux.from(body)).build();

                    Mono modifiedBody = clientResponse.bodyToMono(inClass).flatMap(originalBody -> {
                        // TODO:此次可以对返回的body进行操作
                            String resBody = originalBody.toString();
                            Timestamp timestamp=new Timestamp(System.currentTimeMillis());//转换成系统时间时分秒
                            log.info("响应返回信息:timestamp{},resBody={}",timestamp,resBody);
                            JSONObject json = JSONObject.parseObject(resBody);
                            String identifier = json.getString("identifier");
                            if(StringUtils.isEmpty(identifier)){
                                //因为拿不到identifier,所以只进行第一层加密。大数据埋点信息接口也是一层加密的。
                                originalBody = checkAuthUtils.firstEncrypt(resBody);
                            }else{
                                //按app的加密方式返回
                                log.debug("common服务的响应返回信息经过与app约定的2层加密规则加密再返回");
                                originalBody = checkAuthUtils.encrypt(resBody);
                            }
                        return Mono.just(originalBody);
                    });

                    BodyInserter bodyInserter = BodyInserters.fromPublisher(modifiedBody, outClass);
                    CachedBodyOutputMessage outputMessage =
                            new CachedBodyOutputMessage(exchange, exchange.getResponse().getHeaders());
                    return bodyInserter.insert(outputMessage, new BodyInserterContext()).then(Mono.defer(() -> {
                        Flux<DataBuffer> messageBody = outputMessage.getBody();
                        HttpHeaders headers = getDelegate().getHeaders();
                        headers.setContentType(MediaType.APPLICATION_JSON);
                        if (!headers.containsKey(HttpHeaders.TRANSFER_ENCODING)) {
                            messageBody =
                                    messageBody.doOnNext(data -> headers.setContentLength(data.readableByteCount()));
                        }
                        return getDelegate().writeWith(messageBody);
                    }));
                }

                @Override
                public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
                    return writeWith(Flux.from(body).flatMapSequential(p -> p));
                }
            };
        }

        @Override
        public int getOrder() {
            return NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER -1;
        }
    }


    }
*/
